/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.moltos.parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * BOT_CHAT Parser
 * @author MoLt1eS
 */


public final class ChatParser {

    public File file;
    public String name;
    public String name_target;
    public boolean start;
    public int nLines;
    public List<String> lines = new ArrayList<String>();
    
    public ChatParser(File file){
        this.file = file;
        StartParsing();
    }
    
    
    /**
     * <pre>
     * Initializes the parsing of the file given on the constructor.
     * </pre>
     */
    public void StartParsing(){
        BufferedReader br = null;
        String line;
        String[] parts;
        try {
            br = new BufferedReader(new FileReader(file));
            
            try {
                while ((line = br.readLine()) != null) {
                    
                    parts = line.split("=");
                  
                    switch(getType(parts[0])){
                        case 0:
                                name=parts[1];
                                break;
                        case 1:
                                name_target=parts[1];
                                break;
                        case 2:
                                start = Boolean.valueOf(parts[1]);
                                break;
                        case 3:
                                nLines = Integer.parseInt(parts[1]);
                                break;
                        case 4:
                                lines.add(parts[1]);    
                                break;
                        
                        default:
                                System.err.println("Error parsing given line!");
                                break;
                    }
                    
                }
            } catch (IOException ex) {
                Logger.getLogger(ChatParser.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ChatParser.class.getName()).log(Level.SEVERE, "File Doesn't exist", ex);
        } finally {
            try {
                if(br != null)
                    br.close();
            } catch (IOException ex) {
                Logger.getLogger(ChatParser.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

}
    
    /**
     * 
     * @param string
     * @return
     * <pre>
     * Types:
     * INT  STRING
     * 0    #BOT_NAME
     * 1    #TARGET_BOT_NAME
     * 2    #BOOLEAN_START
     * 3    #NUMBER_LINE
     * 4    #LINE
     * -1   UNKNOWN
     * </pre>
     */
    public int getType(String string) {
        if(string.equalsIgnoreCase("#bot_name"))
            return 0;
        else if(string.equalsIgnoreCase("#target_bot_name"))
            return 1;
        else if(string.equalsIgnoreCase("#boolean_start"))
            return 2;
        else if(string.equalsIgnoreCase("#number_line"))
            return 3;
        else if(string.equalsIgnoreCase("#line"))
            return 4;
        
        return -1;
    }
    
    
    /**
     * 
     * @return 
     */
    public String getName() {
        return name;
    }

    /**
     * 
     * @return 
     */
    public String getName_target() {
        return name_target;
    }

    /**
     * 
     * @return 
     */
    public boolean isStart() {
        return start;
    }

    /**
     * 
     * @return 
     */
    public int getnLines() {
        return nLines;
    }

    /**
     * 
     * @return 
     */
    public List<String> getLines() {
        return lines;
    }
    

}
